﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth]
    public class ResourcesController : BaseController
    {
        //
        // GET: /Resources/
        [StatFilter]
        [UrlStatFilter]
        public ActionResult Index(byte? type, byte? minType, int? isIcon)
        {
            isIcon = isIcon.CheckInt(0);
            type = type.CheckByte(1);
            minType = minType.CheckByte(1);
            ViewData["minType"] = minType.Value;
            ViewData["isIcon"] = isIcon.Value;
            var info = CommonResourcesBLL.Get(type.Value, minType.Value);
            var viewName = ResourcesHelper.GetTypeByEn(type.Value);
            if (type == 1)
                viewName = "Index";
            if (info == null)
            {
                return View(viewName, new CommonResourcesInfo { Type = type.Value, MinType = minType.Value });
            }
            return View(viewName, info);
        }

        public ActionResult TreeUc(byte type, byte minType, int? isIcon)
        {
            ViewData["type"] = type;
            ViewData["minType"] = minType;
            isIcon = isIcon.CheckInt(0);
            ViewData["isIcon"] = isIcon.Value;
            ViewData.Model = ResourcesHelper.GetTextType(true);
            return PartialView();
        }

        public ActionResult CommonListUc(int? pid, string search, string keySearch, byte minType, byte type, int? isIcon)
        {
            CommonRequest.ClearCache();
            isIcon = isIcon.CheckInt(0);
            var list = CommonResourcesBLL.GetList(type, minType, search, keySearch, 1).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            if (isIcon == 0)
                return PartialView(ResourcesHelper.GetTypeByEn(type) + "ListUc", model);
            return PartialView(ResourcesHelper.GetTypeByEn(type) + "ViewListUc", model);
        }

        [StatFilter]
        public ActionResult View(int id)
        {
            var info = CommonResourcesBLL.Get(id);
            if (info == null)
                return NotFound();
            //更新点击数
            CommonResourcesBLL.UpdateHit(id);
            // if (info.Type < 6)
            return View(ResourcesHelper.GetTypeByEn(info.Type) + "View", info);
            // return Redirect("~/" + info.FileName);
        }


        [UserAuth(IsLimt = true, UserType = UserTypeEnum.Student)]
        public ActionResult CommonEdit(byte? type)
        {
            if (!type.HasValue)
                return NotFound();
            var reView = ResourcesHelper.GetTypeByEn(type.Value) + "Edit";

            return View(reView, new CommonResourcesInfo { Type = type.Value });
        }

        [HttpPost]
        [ValidateInput(false)]
        [UserAuth(IsLimt = true, UserType = UserTypeEnum.Student)]
        public ActionResult CommonEdit(CommonResourcesInfo info)
        {
            var viewRe = View(ResourcesHelper.GetTypeByEn(info.Type) + "Edit");
            ViewData.Model = info;
            if (string.IsNullOrEmpty(info.Author) && string.IsNullOrEmpty(info.Source))
                ModelState.AddModelError("Author", "需填写作者或来源");

            if (!ModelState.IsValid)
                return viewRe;
            info.FileName = info.FileName ?? "";
            info.Author = info.Author ?? "";
            info.Message = info.Message ?? "";
            info.Title = info.Title ?? "";
            info.Source = info.Source ?? "";
            info.Keyword = info.Keyword ?? "";
            info.Introduction = info.Introduction ?? "";
            info.MinType = 2;

            var msg = "";
            var fileName = ResourcesHelper.UploadFile(Request.Files["UpFile"], info.Type, out msg, info.Id == 0);
            if (!string.IsNullOrEmpty(msg))
            {
                SetAlertMsg(msg);
                return viewRe;
            }
            info.FileName = fileName;


            info.UploaderId = UserHelper.LoginUser.Id;
            info.UploaderType = 1;
            info.UploadDate = DateTime.Now;
            info.Hits = 0;
            info.IsReview = false;
            if (CommonResourcesBLL.Add(info))
            {
                SetAlertMsg("添加成功.请等待审核!", true);
                return RedirectToAction("Index", new { type = info.Type });
            }
            SetAlertMsg("未知错误.", true);
            return viewRe;
        }

    }
}
